package com.lty.practice.controller;

import com.lty.practice.model.Coffee;
import com.lty.practice.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class PracticeController {
    @Autowired
    private CoffeeService coffeeService;

    @GetMapping(value = "/coffee/one/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Coffee getCoffeeById(@PathVariable Long id) {
        Coffee coffee = coffeeService.selectOneById(id);
        log.info("Coffee {}:", coffee);
        return coffee;
    }

    @GetMapping(value = "/coffee/search", produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public List<Coffee> searchCoffee(@RequestParam String name) {
        Coffee coffee = new Coffee();
        coffee.setName(name);
        return coffeeService.selectByCondition(coffee);
    }
}
